class assignDriver
{
    static getInstance()
    {
        if(!assignDriver.instance)
        {
            assignDriver.instance = new assignDriver();
        }
        return assignDriver.instance;
    }

    /**
     * 分配司机
     * @param points
     */
    assign()
    {
         let routers = this.getRouters();
         for (let routerId in routers)
         {
             let isPush = false;
             //是否有司机在区域范围内
             for (let i=0;i < OBJECT.driverList.length;i++ )
             {
                  if(OBJECT.driverList[i].end_location == 'Home')
                  {
                      /**
                       * 1 把路线的定点画一个多边形
                       * 2 判断司机的位置是否在这个多边形内部
                       */
                      let polygon = qhPlotConvexHull( routers[routerId] );
                      if( Polygon.isInArea( {lat:OBJECT.driverList[i].lat,lng:OBJECT.driverList[i].lng} , polygon) )
                      {

                          if( isPush === false )
                          {
                              if( !this.isAssignedDriver(OBJECT.driverList[i].driver_id , OBJECT.driverMapToRotuer) )
                              {
                                  OBJECT.driverMapToRotuer.push({driver_id:OBJECT.driverList[i].driver_id,router_id:routerId});
                                  isPush = true;
                              }
                          }
                      }
                  }
             }
             //如果没有司机在范围内，则随机分配一个最后一个回仓库的人
             if( isPush === false )
             {
                 for (let i=0;i < OBJECT.driverList.length;i++ )
                 {
                          if( !this.isAssignedDriver(OBJECT.driverList[i].driver_id , OBJECT.driverMapToRotuer) )
                          {
                              OBJECT.driverMapToRotuer.push({driver_id:OBJECT.driverList[i].driver_id,router_id:routerId});
                              break;
                          }

                 }
             }
         }
         this.updateRouteDriver(OBJECT.driverMapToRotuer);
         this.setRouterDriver(OBJECT.driverMapToRotuer);

    }

    /**
     * 设置路线司机
     * @param data
     */
    setRouterDriver(data){
         data.forEach( function ( value ) {
                Router.getInstance().setDriver( value.router_id, value.driver_id );
         })
    }
    /**
     * 更新分配司机
     * @param data
     */
    updateRouteDriver (data)
    {
        $.ajax({
            type: "POST",
            dataType: "json",
            url: CONFIG.updateRouterDriver ,
            data: {data:JSON.stringify(data)},
            success: function (result) {

            },
            error : function() {
                alert('Error:Driver cannot assign');
            }
        });

    }
    /**
     * 获取路线
     * @returns {Array}
     */
    getRouters()
    {
        let routers =[];
        for(let key in OBJECT.routerPonits)
        {
             routers[key]=OBJECT.routerPonits[key].points;
        }
        return routers;
    }

    /**
     * 司机是否已经被分配了
     * @param driver_id
     * @param data
     * @returns {boolean}
     */
    isAssignedDriver(driver_id , data){
        for( let i =0;i<data.length ; i++ )
        {
            if( data[i].driver_id === driver_id )
            {
                return true;
            }
        }
        return false;
    }
}